﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using LTorrent.Extensions;

namespace LTorrent.Peers
{
    class RequestMessage:IPeerMessages
   {
        readonly Byte[] LengthPrefix = ((UInt32)(13)).toNetworkOrder();
        readonly Byte[] Id = { 6 };

        protected MemoryStream _message;

        public RequestMessage(UInt32 index, UInt32 begin, UInt32 length)
        {
            _message = new MemoryStream();
            _message.Write(LengthPrefix, 0, LengthPrefix.Length);
            _message.Write(Id, 0, Id.Length);
            Byte[] Temp = index.toNetworkOrder();
            _message.Write(Temp, 0, Temp.Length);
            Temp = begin.toNetworkOrder();
            _message.Write(Temp, 0, Temp.Length);
            Temp = length.toNetworkOrder();
            _message.Write(Temp, 0, Temp.Length);
        }

        ~RequestMessage()
        {
            _message.Close();
        }

        public byte[] Message
        {
            get { return _message.ToArray(); }
        }
   }
}
